package org.example.filter.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.example.exception.TokenException;
import org.example.utils.RequestContextUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;


@Aspect
@Component
@Slf4j
public class CheckTokenAspect {

    @Pointcut("@annotation(org.example.filter.aop.CheckToken)")
    public void pt(){}

    @Before("pt()")
    public void before(JoinPoint joinPoint){

        String className = joinPoint.getTarget().getClass().getName();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getMethod().getName();
        CheckToken annotation =
                signature.getMethod().getAnnotation(CheckToken.class);
        log.info("类名: {} , 方法名: {} , 注解的值: {}  ",className,methodName,annotation.value());

        String token
                = RequestContextUtil.getRequest().getHeader("Authorization");
        if (!StringUtils.hasText(token)){
            throw new TokenException("未登录的访问!!!");
        }

    }
}
